"""
**自动化新闻摘要与分享**

- 自动打开某新闻APP。
- 浏览或搜索特定类别的新闻。
- 读取新闻标题和内容摘要。
- 使用AI生成该新闻的简短摘要或评论。
- 将摘要或评论自动分享到社交媒体平台（如微信、微博等）。
"""
import time
from typing import Any, Tuple

import uiautomator2 as u2
import MyPackage.Constant as C


class NewsProcessor:
    def __init__(self, device_id: str, summarize_app: str, share_app: str) -> None:
        self.__device = self.__connect_devices(device_id)
        self.__summarize_app = summarize_app
        self.__share_app = share_app
        self.__package, self.__summarize_constant_pool, self.__share_constant_pool = self.__initialize_constant_pool()

    @staticmethod
    def __connect_devices(device_id: str) -> u2.Device:
        try:
            return u2.connect(device_id)
        except Exception as e:
            raise Exception(f'设备连接失败, 具体原因: {e}')

    def __initialize_constant_pool(self) -> Tuple[Any, Any, Any]:
        enum1 = 'App2Package'
        enum2 = self.__summarize_app + 'Constant'
        enum3 = self.__share_app + 'Constant'
        try:
            app2package = getattr(C, enum1)
            summarize_app2constant = getattr(C, enum2)
            share_app2constant = getattr(C, enum3)
            return app2package, summarize_app2constant, share_app2constant
        except AttributeError as e:
            raise AttributeError(f'常量池初始化错误, 具体原因: {e}')

    def __start_summarize_app(self) -> None:
        self.__device.app_start(self.__package[self.__summarize_app].value[0],
                                self.__package[self.__summarize_app].value[1])

    def __stop_summarize_app(self) -> None:
        self.__device.app_stop(self.__package[self.__summarize_app].value[0])

    def __start_share_app(self) -> None:
        self.__device.app_start(self.__package[self.__share_app].value[0],
                                self.__package[self.__share_app].value[1])

    def __stop_share_app(self) -> None:
        self.__device.app_stop(self.__package[self.__share_app].value[0])

    # 摘要提取逻辑
    def __summarize(self) -> None:
        call_summarize_method = '_NewsProcessor__' + self.__summarize_app.lower() + '_summarize'
        if call_summarize_method in NewsProcessor.__dict__:
            summarize_method = getattr(self, call_summarize_method)
            if callable(summarize_method):
                summarize_method()

    # 分享逻辑
    def __share(self) -> None:
        call_share_method = '_NewsProcessor__' + self.__share_app.lower() + '_share'
        if call_share_method in NewsProcessor.__dict__:
            share_method = getattr(self, call_share_method)
            if callable(share_method):
                share_method()

    def __tencentnews_summarize(self) -> None:
        pass

    def __wechat_share(self) -> None:
        pass

    def __click_skip_buton(self, text: str) -> None:
        if self.__device(text=self.__summarize_constant_pool[text].value).wait(4):
            self.__device(text=self.__summarize_constant_pool[text].value).click()

    def __input_search_box_text(self, resource_id: str, search_keyword: str) -> None:
        if self.__device(resourceId=self.__summarize_constant_pool[resource_id].value).wait(2):
            self.__device(resourceId=self.__summarize_constant_pool[resource_id].value).click()
            self.__device.set_input_ime(True)
            time.sleep(2)
            self.__device.send_keys(search_keyword)
            self.__device.set_input_ime(False)

    def __click_search_buton(self, description: str) -> None:
        if self.__device(description=self.__summarize_constant_pool[description].value).wait(2):
            self.__device(description=self.__summarize_constant_pool[description].value).click()

    def __click_classify_button(self, xpath: str) -> None:
        if self.__device(xpath=self.__summarize_constant_pool[xpath].value).wait(2):
            self.__device(xpath=self.__summarize_constant_pool[xpath].value).click()

    def test(self):
        self.__device.xpath(self.__summarize_constant_pool['ConsultingService'].value).click()
        # self.__start_summarize_app()
        # self.__click_skip_buton('SkipBtn')
        # self.__input_search_box_text('SearchBox', '奥运')
        # self.__click_search_buton('SearchBtn')


if __name__ == '__main__':
    o = NewsProcessor('192.168.101.9:5555', 'TencentNews', 'WeChat')
    o.test()

